package zhoug.demo.zui.ui;

import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import zhoug.common.adapters.recycler.TextAdapter;
import zhoug.common.anim.ZAnimations;
import zhoug.common.utils.AppUtils;
import zhoug.demo.zui.R;
import zhoug.zui.ZUIRecyclerView;
import zhoug.zui.dialog.list.ZUIStringListDialog;

/**
 * @Author 35574
 * @Date 2022/4/29
 * @Description
 */
public class TestZUIRecyclerViewActivity extends AppCompatActivity {

    private ZUIRecyclerView mRecyclerView;
    private Button mBtn1;
    private Button mBtn2;
    private Button mBtn3;
    private Button mBtn4;
    private Button mBtn5;
    private Button mBtnData1;
    private Button mBtnData2;
    private Button mBtnData3;
    private Button mBtnData4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_zuirecycler_view_activity);
        findViews();
        init();
    }


    private void findViews() {
        mRecyclerView = findViewById(R.id.recyclerView);
        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);
        mBtn4 = findViewById(R.id.btn4);
        mBtn5 = findViewById(R.id.btn5);
        mBtnData1 = findViewById(R.id.btn_data1);
        mBtnData2 = findViewById(R.id.btn_data2);
        mBtnData3 = findViewById(R.id.btn_data3);
        mBtnData4 = findViewById(R.id.btn_data4);
    }

    private List<String> getData(int count) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            list.add("Item_" + i);
        }
        return list;
    }

    private void refreshData(int count) {
        List<String> data = mListAdapter1.getData();
        data.clear();
        List<String> newData = getData(count);
        if (newData.size() > 0) {
            data.addAll(newData);
        }
        mListAdapter1.notifyDataSetChanged1();

    }

    private TextAdapter<String> mListAdapter1;
    private ZUIStringListDialog mZUIStringListDialog;

    private void init() {
        mBtnData1.setOnClickListener(v -> refreshData(100));
        mBtnData2.setOnClickListener(v -> refreshData(5));
        mBtnData3.setOnClickListener(v -> refreshData(1));
        mBtnData4.setOnClickListener(v -> refreshData(0));

        mListAdapter1 = new TextAdapter<String>() {
            @Override
            public String getText(String s) {
                return s;
            }
        };
        mListAdapter1.setData(getData(100));
        mRecyclerView.setAdapter(mListAdapter1);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mBtn3.setOnClickListener(v -> {
            mZUIStringListDialog = new ZUIStringListDialog(this);
            mZUIStringListDialog.setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
            mZUIStringListDialog.setWindowAnimations(ZAnimations.ANIM_DOWN);
            mZUIStringListDialog.setFullWidth(true);
            mZUIStringListDialog.setCanceledOnTouchOutside(true);
            mZUIStringListDialog.setData(getData(100));
            mZUIStringListDialog.setMaxHeight(getMaxHeight());
            mZUIStringListDialog.show();

        });


    }

    private int getMaxHeight() {
        return AppUtils.getScreenSize().y * 2 / 3;
    }


}
